home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 46
/
Amiga Format CD46 (1999-10-20)(Future Publishing)(GB)[!][issue 1999-12].iso
/
-serious-
/
comms
/
www
/
urlx
/
treecat.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-09-06
|
2KB
|
128 lines
/*
* treecat
*/
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <stdio.h>
#define BUFSIZE 65536
char buf[1024];
char data[BUFSIZE];
int verbose_f;
struct stat st;
void scan_dir(char *path, int pathlen)
{
DIR *dir;
struct direct *dirent;
int oldpathlen;
oldpathlen = pathlen;
if (!(dir = opendir(path)))
{
printf("couldn't open directory '%s'\n", path);
return;
}
#ifdef amigaos
if (pathlen && path[pathlen - 1] != ':' && path[pathlen - 1] != '/')
#endif
path[pathlen++] = '/';
while (dirent = readdir(dir))
{
memcpy(path + pathlen, dirent->d_name, dirent->d_namlen);
path[pathlen + dirent->d_namlen] = 0;
if (stat(path, &st) == 0)
{
if ((st.st_mode & S_IFMT) == S_IFDIR)
{
scan_dir(path, pathlen + dirent->d_namlen);
}
else
{
/*
* file ops go here
*/
/*
* open each file in turn
*/
{
FILE *fp = 0;
if (verbose_f)
fprintf(stderr, "%s\n", path);
if (fp = fopen(path, "rb"))
{
int datalen;
while (datalen = fread(data, 1, BUFSIZE, fp))
{
fwrite(data, 1, datalen, stdout);
}
fclose(fp);
}
}
}
}
else
printf("no stat for %s\n", path);
}
path[oldpathlen] = 0;
closedir(dir);
/*
* directory ops would go here
*/
}
usage()
{
printf("usage: treecat <directoryname>\n");
exit(0);
}
main(int argc, char **argv)
{
int i, patharg = 0;
verbose_f = 0;
for (i = 1; i < argc; i++)
{
if (argv[i][0] == '-')
{
switch (argv[i][1])
{
case 'v': verbose_f++; break;
default: usage();
}
}
else if (patharg == 0)
patharg = i;
else
usage();
}
if (patharg == 0)
usage();
strcpy(buf, argv[patharg]);
scan_dir(buf, strlen(buf));
return (0);
}